<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\Dashboard;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;

class HomeController extends Controller
{
    public function index(Content $content)
    {
        // echo 222;exit;
        return $content
            ->title('概要')
            ->description('实时概况')
            ->row(function (Row $row) {

                $row->column(4, function (Column $column) {
                    $envs = [
                        ['name' => '订单数',       'value' => \DB::table('users_orders')->whereDate('created_at', date('Y-m-d'))->count()],
                        ['name' => '订单数（昨日）',   'value' => \DB::table('users_orders')->whereDate('created_at', date('Y-m-d', strtotime('-1 day', time())))->count()],
                        ['name' => '在线任务',       'value' => \DB::table('subsidy')->where('status', 1)->where('is_frozen', 0)->count()],
                        ['name' => '在线任务（昨日）',   'value' => 0],
                        ['name' => '访客数',       'value' => \DB::table('users_use_log')->whereDate('created_at', date('Y-m-d'))->count()],
                        ['name' => '访客数（昨日）',   'value' => \DB::table('users_use_log')->whereDate('created_at', date('Y-m-d', strtotime('-1 day', time())))->count()],
                        ['name' => '累计客户数',       'value' => \DB::table('users')->count()],
                        ['name' => '累计客户数（今日新增）',   'value' => \DB::table('users')->whereDate('created_at', date('Y-m-d'))->count()],
                    ];
            
                    $e = view('admin::dashboard.environment', compact('envs'));
                    $column->append($e);
                });

                $row->column(4, function (Column $column) {
                    $envs = [
                        ['name' => '结算金额',       'value' => \DB::table('settlement_order')->whereDate('created_at', date('Y-m-d'))->sum('amount')],
                        ['name' => '结算金额（昨日）',   'value' => \DB::table('settlement_order')->whereDate('created_at', date('Y-m-d', strtotime('-1 day', time())))->sum('amount')],
                        ['name' => '收入金额',       'value' => \DB::table('platform_balance_log')->whereDate('created_at', date('Y-m-d'))->whereIn('type', [10, 6, 7])->sum('amount')],
                        ['name' => '收入金额（昨日）',   'value' => \DB::table('platform_balance_log')->whereDate('created_at', date('Y-m-d', strtotime('-1 day', time())))->whereIn('type', [10, 6, 7])->sum('amount')],
                        ['name' => '新增任务',       'value' => \DB::table('subsidy')->whereDate('created_at', date('Y-m-d'))->count()],
                        ['name' => '新增任务（昨日）',   'value' => \DB::table('subsidy')->whereDate('created_at', date('Y-m-d', strtotime('-1 day', time())))->count()],
                        ['name' => '累计商户数',       'value' => \DB::table('users')->where('merchant_status', 1)->count()],
                        ['name' => '累计商户数（今日新增）',   'value' => \DB::table('users')->where('merchant_status', 1)->whereDate('created_at', date('Y-m-d'))->count()],
                    ];
            
                    $e = view('admin::dashboard.environment', compact('envs'));
                    $column->append($e);
                });

                $row->column(4, function (Column $column) {
                    $usersCount = \DB::table('users')->count();
                    $usersOrderCount = \DB::table('users')->where('last_order_time', '<>', null)->count();
                    $usersTomoOrderCount = \DB::table('users_orders')->whereDate('created_at', date('Y-m-d', strtotime('-1 day', time())))->groupBy('uid')->count();

                    $usersVipOrderCount = \DB::table('users')->where('last_order_time', '<>', null)->where('vip_status', 1)->count();
                    $usersNotVipOrderCount = \DB::table('users')->where('last_order_time', '<>', null)->where('vip_status', 0)->count();

                    $envs = [
                        ['name' => '下单率',       'value' => $usersCount?(round($usersOrderCount/$usersCount, 2)*100)."%":0],
                        ['name' => '下单率（昨日）',   'value' => $usersCount?(round($usersTomoOrderCount/$usersCount, 2)*100)."%":0],
                        ['name' => '总成交用户数',       'value' => $usersOrderCount],
                        ['name' => '成交客户数占比（会员）',       'value' => $usersCount?(round($usersVipOrderCount/$usersCount, 2)*100)."%":0],
                        ['name' => '成交客户数占比（非会员）',   'value' => $usersCount?(round($usersNotVipOrderCount/$usersCount, 2)*100)."%":0],
                    ];
            
                    $e = view('admin::dashboard.environment', compact('envs'));
                    $column->append($e);
                });
            });
    }
}
